home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / doln.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  1KB  |  56 lines

  1. #include "kiss.h"
  2.  
  3. int doln (Stringstack s)
  4. {
  5.     register int
  6.     sym = 0,
  7.     forced = 0,
  8.     opt;
  9.     struct stat
  10.     statbuf;
  11.     register char
  12.     *src,
  13.     *dest;
  14.     
  15.     while ( (opt = getopt (s.nstr, s.str, "sfh")) != -1 )
  16.     if (opt == 's')
  17.         sym++;
  18.     else if (opt == 'f')
  19.         forced++;
  20.     else
  21.         error ("Bad commandline.\n"
  22.            "Usage: %s [-sf] srcfile linkname\n"
  23.            "Where:\n"
  24.            "    -s: make symbolic link (default: make hard link)\n"
  25.            "    -f: force symlink even if srcfile doesn't exist\n"
  26.            "    srcfile: file to make link to\n"
  27.            "    linkname: link to create, may not exist (yet)\n"
  28.            , progname);
  29.  
  30.     if (s.nstr - optind != 2)
  31.     error ("need two arguments");
  32.  
  33.     src = s.str [optind];
  34.     dest = s.str [s.nstr - 1];
  35.  
  36.     if (stat (src, &statbuf))
  37.     if (! forced || ! sym)
  38.         error ("can't stat \"%s\"", src);
  39.     if (! stat (dest, &statbuf))
  40.     error ("\"%s\" already exists, can't create that link",
  41.            dest);
  42.  
  43.     if (sym)
  44.     {
  45.     if (symlink (src, dest))
  46.         error ("can't create symlink from \"%s\" to \"%s\"",  src, dest);
  47.     }
  48.     else
  49.     {
  50.     if (link (src, dest))
  51.         error ("can't create hard link from \"%s\" to \"%s\"", src, dest);
  52.     }
  53.     
  54.     return (0);
  55. }
  56.